home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 56194 / 56194.xpi / chrome / topsite.jar / content / js / browserOverlay.js next >
Text File  |  2010-01-17  |  20KB  |  499 lines

  1. if(!topsite) var topsite={};
  2. if(!topsite.overlay) topsite.overlay = {};
  3. if(!topsite.httpRequestObserver) topsite.httpRequestObserver = {};
  4. if(!topsite.webProgressListener) topsite.webProgressListener = {};
  5.  
  6. topsite.overlay = {
  7.     
  8.     displayViews: null,
  9.  
  10.     // Configuration parameters (with default values)
  11.         clientCacheMode: topsite.consts.CACHE_NONE,     
  12.         cacheExpiredForItemInHours: 2,
  13.         maxThumbnailInScreen: 5,
  14.         maxRetriesForRelatedDownload: 5,
  15.         maxRetriesForImageDownload: 5,
  16.         intervalInSecondsForRelatedRetries: 5,
  17.         intervalInSecondsForImageRetries: 5,
  18.         lowLevelToNotifyBackInSeconds: 10,
  19.         highLevelToNotifyBackInSeconds: 40,
  20.  
  21.         currTabIndex: 0,
  22.         prevTabIndex: 0,
  23.     currSelectedDocument: null,
  24.     
  25.     test: function(){ alert('test'); },
  26.  
  27.     initDisplayViews: function(){
  28.  
  29.         topsite.overlay.displayViews = new Array(1);
  30.         topsite.overlay.displayViews[0] = new topsite.toolbarView(document.getElementById("topsiteTopicsContainer"), topsite.overlay.getTopSiteForTopic, topsite.overlay.siteClickedInView);
  31.     },
  32.  
  33.     browserInit: function(){
  34.         
  35.  
  36.         topsite.overlay.initDisplayViews();
  37.         
  38.         topsite.dal.init();
  39.  
  40.         var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  41.             observerService.addObserver(topsite.httpRequestObserver, "http-on-modify-request", false);
  42.                 
  43.             gBrowser.addProgressListener(topsite.webProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
  44.  
  45.         gBrowser.addEventListener("pageshow", topsite.overlay.pageShow, true);     
  46.  
  47.         // Register Tab events
  48.             var container = gBrowser.tabContainer;
  49.         container.addEventListener("TabOpen", topsite.overlay.browserTabAdded, false);
  50.         container.addEventListener("TabMove", topsite.overlay.browserTabMoved, false);
  51.         container.addEventListener("TabClose", topsite.overlay.browserTabRemoved, false);
  52.         container.addEventListener("TabSelect", topsite.overlay.browserTabSelected, false);            
  53.     },    
  54.     browserUninit: function(){
  55.             var container = gBrowser.tabContainer;
  56.             container.removeEventListener("TabOpen", topsite.overlay.browserTabAdded, false);
  57.             container.removeEventListener("TabMove", topsite.overlay.browserTabMoved, false);
  58.             container.removeEventListener("TabClose", topsite.overlay.browserTabRemoved, false);
  59.             container.removeEventListener("TabSelect", topsite.overlay.browserTabSelected, false);
  60.                 gBrowser.removeProgressListener(topsite.webProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
  61.         gBrowser.removeEventListener("pageshow", topsite.overlay.pageShow, true);        
  62.     },
  63.  
  64.     browserTabAdded: function(event){
  65.     },    
  66.     browserTabMoved: function(event){
  67.     },    
  68.  
  69.     lastTabRemovedIdx: -1,
  70.     mTabIndex: 0,
  71.  
  72.     browserTabRemoved: function(event){
  73.         with(topsite.overlay){       
  74.             var index = gBrowser.getBrowserIndexForDocument(event.target.linkedBrowser.contentDocument);               
  75.                 if (index == mTabIndex){
  76.                     if (index < mTabIndex)
  77.                         mTabIndex = mTabIndex - 1;                        
  78.                     currSelectedDocument = gBrowser.getBrowserAtIndex(mTabIndex).contentDocument;            
  79.                     lastTabRemovedIdx = index;            
  80.                     prevTabIndex = currTabIndex;
  81.                     currTabIndex = mTabIndex;
  82.             }
  83.         }        
  84.     },
  85.     
  86.     browserTabSelected: function(event){
  87.         var tabIndex = gBrowser.getBrowserIndexForDocument(event.target.linkedBrowser.contentDocument);        
  88.         setTimeout(function(){
  89.         with(topsite.overlay){
  90.             if (parseInt(lastTabRemovedIdx) >= 0 && parseInt(lastTabRemovedIdx) < parseInt(tabIndex)){
  91.                     tabIndex = tabIndex - 1;
  92.             }        
  93.             lastTabRemovedIdx = -1;        
  94.             mTabIndex = tabIndex;        
  95.             currSelectedDocument = gBrowser.getBrowserAtIndex(mTabIndex).contentDocument;        
  96.             prevTabIndex = currTabIndex;
  97.             currTabIndex = mTabIndex;
  98.         clearPrevResultsInViews();
  99.         var tabObject = gBrowser.getBrowserAtIndex(tabIndex);                
  100.         var currTopics = tabObject.getAttribute("Topics");
  101.         if (currTopics != ""){
  102.             var arrTopicsForUrl = currTopics.split(",");
  103.             topsite.overlay.populateTopicsInViews(arrTopicsForUrl);
  104.         }        
  105.         }
  106.         }, 0);
  107.     },
  108.     
  109.     pageShowDoc: null,
  110.  
  111.     pageShow: function(event){
  112.         var doc = event.originalTarget;           
  113.             if (doc instanceof HTMLDocument){
  114.                 if (doc.defaultView.frameElement){
  115.                       while (doc.defaultView.frameElement){
  116.                             doc=doc.defaultView.frameElement.ownerDocument;
  117.                       }
  118.                 }        
  119.                 var tabIndex = gBrowser.getBrowserIndexForDocument(doc);        
  120.                 if (parseInt(tabIndex) < 0)
  121.                         return;
  122.                 var docMainWin = gBrowser.getBrowserAtIndex(tabIndex).contentDocument;            
  123.                 if (docMainWin != doc)            
  124.                         return;                
  125.                 topsite.overlay.pageShowDoc = doc;
  126.     
  127.                 setTimeout(function(){
  128.                 if (doc.location){
  129.                             topsite.overlay.pageShowCheckRedirect(doc.location.href);
  130.                 }
  131.                     }, 1000);
  132.             }
  133.       },
  134.   
  135.     pageShowCheckRedirect: function(newLoc){
  136.             var TsIsRedirect = false;
  137.             var TsRedirectFrom = null;            
  138.                 if (topsite.overlay.pageShowDoc.location != null){
  139.                     if (topsite.overlay.pageShowDoc.location.href != newLoc){
  140.                             TsIsRedirect = true;                    
  141.                             TsRedirectFrom = newLoc;
  142.                     }                
  143.                     topsite.overlay.processNewURL(topsite.overlay.pageShowDoc, topsite.overlay.pageShowDoc.location.href, TsIsRedirect, TsRedirectFrom, true);
  144.                 }    
  145.   
  146.     },
  147.  
  148.     processNewURL: function(p_doc, p_newUrl, p_bTsIsRedirect, p_redirectedFrom, p_TsIsPageLoaded, referrer) {
  149.   
  150.         if (p_doc instanceof HTMLDocument) {
  151.             
  152.             var doc = p_doc;            
  153.             
  154.             if (doc.defaultView && doc.defaultView.frameElement) {
  155.               
  156.               while (doc.defaultView.frameElement) {
  157.                 doc=doc.defaultView.frameElement.ownerDocument;
  158.                 p_newUrl = doc.location;
  159.               }
  160.             }
  161.  
  162.             var tabIndex = gBrowser.getBrowserIndexForDocument(doc);
  163.     
  164.             if (parseInt(tabIndex) < 0){
  165.                 return;
  166.             }
  167.             
  168.             try{
  169.                 if (p_newUrl.substring(0, 4) != "http"){
  170.                     return;
  171.                 }
  172.             }catch(e3){}
  173.  
  174.             var docMainWin = gBrowser.getBrowserAtIndex(tabIndex).contentDocument;
  175.                 
  176.             if (docMainWin != doc){
  177.                 return;
  178.             }                                      
  179.            
  180.             var tabObject = gBrowser.getBrowserAtIndex(tabIndex);            
  181.             var newUrl = p_newUrl;                        
  182.             var currUrl = tabObject.getAttribute("TsCurrentUrl");            
  183.             var blnNewTab = false;
  184.             
  185.             if (currUrl == ""){
  186.                 var prevTabObject = null;
  187.  
  188.                 if (topsite.overlay.currTabIndex == tabIndex){
  189.                     prevTabObject = gBrowser.getBrowserAtIndex(topsite.overlay.prevTabIndex);
  190.                 }
  191.                 else{
  192.                     prevTabObject = gBrowser.getBrowserAtIndex(topsite.overlay.currTabIndex);
  193.                 }        
  194.                             
  195.                 if (prevTabObject != null){
  196.                     tabObject.setAttribute("TsCurrentUrl", prevTabObject.getAttribute("TsCurrentUrl"));
  197.                     tabObject.setAttribute("TsOldUrl", prevTabObject.getAttribute("TsOldUrl"));
  198.                     
  199.                     currUrl = tabObject.getAttribute("TsCurrentUrl");
  200.                     
  201.                     blnNewTab = true;
  202.                 }
  203.             }
  204.  
  205.  
  206.             var blnTsIsLink = false;
  207.             
  208.             if (referrer != null){
  209.                 if (referrer.spec == currUrl){                 
  210.                     blnTsIsLink = true;
  211.                 }
  212.                 else if (referrer.spec.indexOf(encodeURIComponent(newUrl)) > 0){
  213.                     blnTsIsLink = true;
  214.                 }
  215.                 else if (referrer.spec.indexOf(encodeURIComponent(newUrl).replace(/~/g, "%257E")) > 0){
  216.                     blnTsIsLink = true;
  217.                 }
  218.                 else if (p_bTsIsRedirect && referrer.spec.indexOf(encodeURIComponent(p_redirectedFrom)) > 0){
  219.                     blnTsIsLink = true;
  220.                 }
  221.                 else if (p_bTsIsRedirect && referrer.spec.indexOf(encodeURIComponent(p_redirectedFrom).replace(/~/g, "%257E")) > 0){
  222.                     blnTsIsLink = true;
  223.                 }
  224.             }
  225.             
  226.         
  227.             if (currUrl != null){
  228.                 if (!blnNewTab && currUrl == newUrl){
  229.                     if (p_TsIsPageLoaded){
  230.                         tabObject.setAttribute("TsIsPageLoaded", true);                        
  231.                         //topsite.overlay.siteLoadedInBrowser(tabIndex);                         
  232.                     }
  233.                     
  234.                     return;
  235.                 }                
  236.             }
  237.             
  238.             if (p_TsIsPageLoaded){
  239.                 tabObject.setAttribute("TsIsPageLoaded", true);
  240.             }
  241.             else{
  242.                 tabObject.setAttribute("TsIsPageLoaded", false);
  243.             }           
  244.                         
  245.             var TsNavType = tabObject.getAttribute("TsNavType");                
  246.             var prevTsNavType = tabObject.getAttribute("PrevTsNavType");                
  247.             
  248.             if (TsNavType == null)
  249.                 TsNavType = topsite.consts.NAV_TYPE_REGULAR;
  250.             if (prevTsNavType == null)
  251.                 prevTsNavType = topsite.consts.NAV_TYPE_REGULAR;
  252.                         
  253.             tabObject.setAttribute("TsNavType", TsNavType); 
  254.             
  255.             tabObject.setAttribute("TsIsRedirect", p_bTsIsRedirect);
  256.             
  257.         if (p_bTsIsRedirect){
  258.                 tabObject.setAttribute("TsRedirectFrom", p_redirectedFrom);
  259.             }            
  260.             
  261.             if (!p_bTsIsRedirect){
  262.                 tabObject.setAttribute("TsOldUrl", tabObject.getAttribute("TsCurrentUrl"));
  263.             }
  264.             else {    
  265.                 if (tabObject.getAttribute("TsIsLink") == "true"){
  266.                     blnTsIsLink = true;
  267.                 }
  268.             }
  269.             
  270.             tabObject.setAttribute("TsCurrentUrl", newUrl);
  271.             tabObject.setAttribute("TsIsLink", blnTsIsLink);
  272.         tabObject.setAttribute("TsgotResponse", false);
  273.             
  274.         tabObject.setAttribute("PrevTsNavType", TsNavType);
  275.             tabObject.setAttribute("TsNavType", topsite.consts.NAV_TYPE_REGULAR);
  276.                         
  277.             var TsOldUrl = tabObject.getAttribute("TsOldUrl");
  278.                 
  279.         topsite.overlay.getSiteTopics(newUrl, TsOldUrl, TsNavType, tabIndex, false, blnTsIsLink);                    
  280.  
  281.             }
  282.         
  283.     },
  284.  
  285.     clearPrevResults: function(tabIndex){
  286.         if (tabIndex == gBrowser.tabContainer.selectedIndex){
  287.             topsite.overlay.clearPrevResultsInViews();                        
  288.         }
  289.         
  290.         var tabObject = gBrowser.getBrowserAtIndex(tabIndex);                
  291.         tabObject.setAttribute("Topics", "");
  292.     },
  293.  
  294.     clearPrevResultsInViews: function(){
  295.         for (var i = 0; i < topsite.overlay.displayViews.length; ++i){
  296.             var displayView = topsite.overlay.displayViews[i];
  297.             displayView.clear();
  298.         }
  299.     },
  300.  
  301.     getSiteTopics: function(newUrl, prevUrl, TsNavType, tabIndex, force, isFromLink){
  302.         
  303.         topsite.overlay.clearPrevResults(tabIndex);
  304.  
  305.         var cancelAdv = false;
  306.         if (TsNavType == topsite.consts.NAV_TYPE_ADV)
  307.             cancelAdv = true;
  308.                 
  309.                 var arrExtraData = new Array(2);
  310.                 arrExtraData[0] = newUrl;
  311.                 arrExtraData[1] = tabIndex;                
  312.  
  313.         topsite.dal.getTopicsForUrl(newUrl, prevUrl, TsNavType, isFromLink, cancelAdv, topsite.overlay.setSiteTopics, arrExtraData);
  314.  
  315.     },
  316.  
  317.     setSiteTopics: function(arrTopicsForUrl, arrExtraData){
  318.         
  319.                 var newUrl = arrExtraData[0];
  320.                 var tabIndex = arrExtraData[1];
  321.  
  322.         var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
  323.                 
  324.         if (tabObject.getAttribute("TsCurrentUrl") == newUrl){
  325.             tabObject.setAttribute("Topics", arrTopicsForUrl.join(","));
  326.             tabObject.setAttribute("TsgotResponse", true);
  327.  
  328.             if (tabIndex == gBrowser.tabContainer.selectedIndex){
  329.                 topsite.overlay.populateTopicsInViews(arrTopicsForUrl);
  330.             }
  331.         }
  332.     },
  333.  
  334.     populateTopicsInViews: function(arrTopicsForUrl){
  335.         for (var i = 0; i < topsite.overlay.displayViews.length; ++i){
  336.             var displayView = topsite.overlay.displayViews[i];
  337.             displayView.populate(arrTopicsForUrl);
  338.         }
  339.     },
  340.  
  341.     getTopSiteForTopic: function(strTopic, objState){
  342.         
  343.         var tabIndex = gBrowser.tabContainer.selectedIndex;
  344.         var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
  345.         var strCurrUrl = tabObject.getAttribute("TsCurrentUrl");
  346.  
  347.         var arrExtraData = new Array(3);
  348.                 arrExtraData[0] = strCurrUrl;
  349.                 arrExtraData[1] = tabIndex;
  350.         arrExtraData[2] = objState; 
  351.  
  352.         topsite.dal.getTopSiteForTopic(strTopic, strCurrUrl, topsite.overlay.setTopSiteForTopic, arrExtraData);
  353.     },
  354.  
  355.     setTopSiteForTopic: function(arrTopSiteUrls, arrExtraData){
  356.         var currUrl = arrExtraData[0];
  357.                 var tabIndex = arrExtraData[1];
  358.         var objState = arrExtraData[2];
  359.  
  360.         var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
  361.                 
  362.         if (tabObject.getAttribute("TsCurrentUrl") == currUrl){
  363.             if (tabIndex == gBrowser.tabContainer.selectedIndex){
  364.                 for (var i = 0; i < topsite.overlay.displayViews.length; ++i){
  365.                     var displayView = topsite.overlay.displayViews[i];
  366.                     displayView.showTopSite(arrTopSiteUrls, objState);
  367.                 }    
  368.             }
  369.         }
  370.     },
  371.  
  372.     siteClickedInView: function(strUrl, siteTopic, isAdv){
  373.         var tabIndex = gBrowser.tabContainer.selectedIndex;
  374.         var tabObject = gBrowser.getBrowserAtIndex(tabIndex);
  375.         if (isAdv){
  376.             tabObject.setAttribute("TsNavType", topsite.consts.NAV_TYPE_ADV);
  377.         }else{
  378.             tabObject.setAttribute("TsNavType", topsite.consts.NAV_TYPE_CLICKED);
  379.         }
  380.         
  381.         topsite.overlay.openUrlInBrowser(strUrl, siteTopic);
  382.     },
  383.  
  384.     findTopSite: function(){
  385.         var term = encodeURIComponent(document.getElementById('topsiteSearchBox').value);
  386.         topsite.overlay.findTopSiteForTerm(term);
  387.     },
  388.  
  389.     findTopSiteForTerm: function(term){
  390.         var searchUrl = "http://www.topsite.com/search?sterm=" + term + "&stype=5";
  391.         topsite.overlay.openUrlInBrowser(searchUrl);
  392.     },
  393.  
  394.     openUrlInBrowser: function(strUrl, siteTopic){
  395.         var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  396.         var uriRef = ios.newURI("http://www.topsite.com/", null, null);
  397.         if (siteTopic)
  398.                     uriRef = ios.newURI("http://www.topsite.com/best/" + siteTopic, null, null);
  399.             
  400.         var objNewTab = gBrowser.addTab();            
  401.                 gBrowser.getBrowserForTab(objNewTab).loadURI(encodeURI(strUrl), uriRef);                        
  402.                 gBrowser.selectedTab = objNewTab;            
  403.     }    
  404. };
  405.  
  406. topsite.httpRequestObserver = {
  407.     getDocfromDOM: function(aChannel, aSubject) {
  408.             try {
  409.                     if(aChannel.notificationCallbacks)
  410.                         var notificationCallbacks = aChannel.notificationCallbacks;
  411.                     else
  412.                         var notificationCallbacks = aSubject.loadGroup.notificationCallbacks;
  413.                 var callback = notificationCallbacks.getInterface(Components.interfaces.nsIDOMWindow);
  414.                     try {
  415.                         if(callback.document != null) 
  416.                                 var doc = callback.document;
  417.                     } catch (e) {             
  418.                         return null;
  419.                     }
  420.             } catch (e) {            
  421.                 return null;
  422.             }
  423.             return doc;
  424.     },
  425.     
  426.     observe : function(aSubject, aTopic, aData) {
  427.             try
  428.             {
  429.  
  430.                     if (aSubject != null){
  431.                         var aChannel = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
  432.                 
  433.                         var request = aChannel.QueryInterface(Components.interfaces.nsIRequest); 
  434.  
  435.                         if (request.loadFlags & 2686976){
  436.                                 var doc = topsite.httpRequestObserver.getDocfromDOM(aChannel, aSubject);
  437.  
  438.                                 if (doc != null){
  439.                                     if (request.loadFlags & 262144){
  440.                             setTimeout(function(){
  441.                                             topsite.overlay.processNewURL(doc, aChannel.URI.spec, true, aChannel.originalURI.spec, false, aChannel.referrer);    
  442.                             },0);
  443.                                     } else {
  444.                             setTimeout(function(){
  445.                                                 topsite.overlay.processNewURL(doc, aChannel.URI.spec, false, null, false, aChannel.referrer);
  446.                             },0);
  447.                                     }
  448.                                 }
  449.                         }
  450.                     }
  451.  
  452.             } catch (e) {}    
  453.     }
  454. };
  455.  
  456. topsite.webProgressListener = {
  457.     SW_STATE_START: 0x00000001,
  458.         SW_STATE_REDIRECTING: 0x00000002,
  459.         SW_STATE_TRANSFERRING: 0x00000004,
  460.         SW_STATE_STOP: 0x00000010,
  461.         SW_STATE_IS_REQUEST: 0x00010000,
  462.         SW_STATE_IS_DOCUMENT: 0x00020000,
  463.         SW_STATE_IS_NETWORK: 0x00040000,
  464.         SW_STATE_IS_WINDOW: 0x00080000, 
  465.     
  466.       QueryInterface: function(aIID) {
  467.         if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  468.                    aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  469.                    aIID.equals(Components.interfaces.nsISupports))
  470.                  return topsite.overlay;
  471.            throw Components.results.NS_NOINTERFACE;
  472.       },
  473.  
  474.     onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
  475.             try {        
  476.                 if (aFlag & topsite.webProgressListener.SW_STATE_IS_WINDOW) {
  477.                         if((aFlag & topsite.webProgressListener.SW_STATE_START) && (aFlag & topsite.webProgressListener.SW_STATE_IS_DOCUMENT)) {
  478.                             topsite.overlay.processNewURL(aWebProgress.DOMWindow.document, aRequest.name, false, null, false);
  479.                         }
  480.                     else if (aFlag & topsite.webProgressListener.SW_STATE_STOP){
  481.                             var aChannel = aRequest.QueryInterface(Components.interfaces.nsIChannel);
  482.                             if (!(aChannel.originalURI.equals(aChannel.URI))) {
  483.                                     topsite.overlay.processNewURL(aWebProgress.DOMWindow.document, aChannel.URI.spec, true, aChannel.originalURI.spec, false);
  484.                             }                 
  485.                         }
  486.                 }   
  487.             } catch (e) {}
  488.             return 0;
  489.     },
  490.  
  491.     onLocationChange: function(aProgress, aRequest, aURI) { return 0; },
  492.     onProgressChange: function( webProgress , request , curSelfProgress , maxSelfProgress , curTotalProgress , maxTotalProgress ) { return 0; },
  493.     onStatusChange: function() {return 0;},
  494.     onSecurityChange: function() {return 0;},
  495.     onLinkIconAvailable: function() {return 0;}
  496. };
  497.  
  498. window.addEventListener("load", function() {topsite.overlay.browserInit();}, false);
  499. window.addEventListener("unload", function() {topsite.overlay.browserUninit();}, false);